//
//  HOEditTableViewCell.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 1/13/16.
//  Copyright © 2016 ShenOu. All rights reserved.
//

#import "HOEditTableViewCell.h"
#import "HOTextField.h"
#import "RatingBar.h"
#import "HOButton.h"
#import "UIImageView+WebCache.h"
#import "HOImageMgr.h"
#import "HOUtils.h"
#import "HODefine.h"

CGFloat kImgInCellWidth = 30;
CGFloat kImgInCellHeight = 30;

@interface HOEditTableViewCell()
{
	UILabel			*detailLbl;
	UILabel			*countLbl;
	HOTextField		*editField;
	UIImageView		*detailImgView;
	BOOL			enableEditText;
	RatingBar		*bar;
	BOOL			needNextIcon;
}

@end
@implementation HOEditTableViewCell
@synthesize isTranslaterDetail;
- (void)initViewsWithHeight:(CGFloat)height withWidth:(CGFloat)width withDelegate:(id<HOEditTableViewCellDelegate>)delegate
{
	self.delegate = delegate;
	[super initViewsWithHeight:height withWidth:width];
}

- (void)createdetailLbl
{
	if(detailLbl)
		return;
	detailLbl = [[UILabel alloc]initWithFrame:CGRectMake(100,
															 (cellHeight - 22) / 2,
															 cellWidth - 100 - 35,
															 1000)];
	detailLbl.textColor = TEXT_GRAY_DARK_COLOR;
	detailLbl.font = [UIFont systemFontOfSize:14];
	detailLbl.numberOfLines = 0;
	detailLbl.textAlignment =  NSTextAlignmentRight;
	
	[self.contentView addSubview:detailLbl];
}

- (void)createCountLbl
{
	if(countLbl)
	{
		countLbl.hidden = YES;
		return;
	}
	countLbl = [[UILabel alloc]initWithFrame:CGRectMake(cellWidth - 55,
														 (cellHeight - 22) / 2,
														 100,
														 22)];
	countLbl.textColor = [UIColor whiteColor];
	countLbl.font = [UIFont systemFontOfSize:13];
	countLbl.numberOfLines = 1;
	countLbl.textAlignment =  NSTextAlignmentCenter;
	countLbl.layer.cornerRadius = 11;
	[countLbl.layer setMasksToBounds:YES];
	[countLbl setBackgroundColor:[UIColor grayColor]];
	[self.contentView addSubview:countLbl];
}

- (void)setCount:(NSInteger)count
{
	countLbl.text = [NSString stringWithFormat:@"%ld", (long)count];
	[countLbl sizeToFit];
	CGFloat width = countLbl.frame.size.width + 10;
	width = MAX(width, 22);
	countLbl.frame = CGRectMake(cellWidth - 40 - width,
								(cellHeight - 22) / 2,
								width,
								22);
}

- (void)showCountLbl:(BOOL)show
{
	countLbl.hidden = !show;
}

- (void)createTextField
{
	if(editField)
		return;
	editField = [[HOTextField alloc]initWithFrame:CGRectMake(40,
															 2,
															 cellWidth - 40,
															 cellHeight)];
	editField.clearButtonMode = UITextFieldViewModeWhileEditing;
	editField.font = [UIFont systemFontOfSize:CELL_FONT_SIZE];
//	editField.layer.borderWidth = 1;
	[self.contentView addSubview:editField];
}


- (void)setDetailImage:(UIImage *)image
{
	[self createDetailImageView];
	[detailImgView setImage:image];
}

- (UIImageView *)getDetailImgView
{
	[self createDetailImageView];
	return detailImgView;
}

- (void)createDetailImageView
{
	if(detailImgView)
		return;
	detailImgView = [[UIImageView alloc]
					 initWithFrame:CGRectMake(cellWidth - HO_HORIZONAL_PADDING - kImgInCellWidth - 25,
											  (cellHeight - kImgInCellHeight) / 2,
											  kImgInCellWidth,
											  kImgInCellHeight)];
	detailImgView.layer.cornerRadius = kImgInCellHeight / 2;
	detailLbl.font = [UIFont systemFontOfSize:CELL_FONT_SIZE];
	detailImgView.image = [[HOImageMgr sharedInstance] getDefaultImage];
	[detailImgView.layer setMasksToBounds:YES];
	detailLbl.hidden = YES;
	[self.contentView addSubview:detailImgView];
}

- (void)setTextEditing:(BOOL)editing
{
	detailLbl.enabled = editing;
	enableEditText = editing;
	nextImgView.hidden = editing;
	self.selectionStyle = editing ? UITableViewCellSelectionStyleGray : UITableViewCellSelectionStyleNone;
//	detailLbl.layer.borderWidth = 1;
	if(editing)
	{
		[self createTextField];
	}
	else
	{
		[self createdetailLbl];
	}
}

- (void)hiddenIcon
{
	iconImgView.hidden = YES;
}
- (void)setNeedNext:(BOOL)needNext
{
	nextImgView.hidden = !needNext;
	needNextIcon = needNext;
	if(!needNext)
	{
		
		detailLbl.frame = CGRectMake(cellWidth - detailLbl.frame.size.width - 10,
													detailLbl.frame.origin.y,
													detailLbl.frame.size.width,
													detailLbl.frame.size.height);
		
	}
	else
	{
		detailLbl.frame = CGRectMake(cellWidth - detailLbl.frame.size.width - 30,
													detailLbl.frame.origin.y,
													detailLbl.frame.size.width,
													detailLbl.frame.size.height);
	}
}

- (void)setContent:(NSString *)content
{
	[super setContent:content];
	[contentLbl sizeToFit];
	nextImgView.hidden = YES;
	contentLbl.frame = CGRectMake(iconImgView.frame.size.width + iconImgView.frame.origin.x + HO_HORIZONAL_PADDING,
								  contentLbl.frame.origin.y,
								  MIN(cellWidth - 130, contentLbl.frame.size.width),
								  22);
	CGFloat x = contentLbl.frame.size.width + contentLbl.frame.origin.x + HO_HORIZONAL_PADDING;
	detailLbl.frame = CGRectMake(x,
										contentLbl.frame.origin.y,
										cellWidth - x - 40,
										100);
//	if(self.isTranslaterDetail)
//	{
//		contentLbl.frame = CGRectMake(iconImgView.hidden ? HO_HORIZONAL_PADDING : 40,
//									  (40 - 22) / 2,
//									  contentLbl.frame.size.width,
//									  22);
//	}
//	else
//	{
//		contentLbl.frame = CGRectMake(iconImgView.frame.size.width + HO_HORIZONAL_PADDING,
//									  (cellHeight - 22) / 2,
//									  contentLbl.frame.size.width,
//									  22);
//	}
}

- (UILabel *)getContentLbl
{
	return contentLbl;
}

- (void)createRatingView
{
	detailLbl.hidden = YES;

	if(bar == nil)
	{
		bar = [[RatingBar alloc] initWithFrame:CGRectMake(cellWidth - 180,
												 (cellHeight - 30) / 2 - 2,
												 90,
												 30)  isSmall:NO];
		bar.starNumber = 5;
		[bar setBackgroundColor:[UIColor clearColor]];
//		bar.layer.borderWidth = 1;
		bar.enable = NO;
		bar.frame = CGRectMake(cellWidth - bar.frame.size.width - 35,
							   (cellHeight - bar.frame.size.height) / 2,
							   bar.frame.size.width,
							   bar.frame.size.height);
		[self.contentView addSubview:bar];
	}
}

- (RatingBar *)getRatingView
{
	return bar;
}
- (UIImageView *)getIconView
{
	return iconImgView;
}


- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
	detailLbl.textAlignment = textAlignment;
}

- (UILabel *)getDetailLbl
{
	return detailLbl;
}

- (void)setDetailContent:(NSString *)content
{
	CGFloat x = contentLbl.frame.size.width + contentLbl.frame.origin.x + HO_HORIZONAL_PADDING;
	detailLbl.frame = CGRectMake(x,
										detailLbl.frame.origin.y,
										cellWidth - x - (needNextIcon ? 40 : HO_HORIZONAL_PADDING),
										detailLbl.frame.size.height);
	detailLbl.text = content;
	[detailLbl sizeToFit];
	if(self.isTranslaterDetail)
	{
		contentLbl.frame = CGRectMake(contentLbl.frame.origin.x,
											(42 - contentLbl.frame.size.height) / 2,
											contentLbl.frame.size.width,
											MAX(contentLbl.frame.size.height, 22));
		detailLbl.frame = CGRectMake(detailLbl.frame.origin.x,
											contentLbl.frame.origin.y,
											cellWidth - x - (needNextIcon ? 40 : HO_HORIZONAL_PADDING),
											MAX(detailLbl.frame.size.height, 22));
		iconImgView.frame = CGRectMake(iconImgView.frame.origin.x,
									   (40 - iconImgView.frame.size.height) / 2,
									   iconImgView.frame.size.width,
									   iconImgView.frame.size.height);
		
	}
	else
	{
		
		detailLbl.frame = CGRectMake(x,
											contentLbl.frame.origin.y,
											cellWidth - x - (needNextIcon ? 40 : HO_HORIZONAL_PADDING),
											MAX(detailLbl.frame.size.height, 22));
	}
}


- (void)setDetailContentNoNewLine:(NSString *)content
{
	CGFloat x = contentLbl.frame.size.width + contentLbl.frame.origin.x + HO_HORIZONAL_PADDING;
	detailLbl.numberOfLines = 1;
	detailLbl.frame = CGRectMake(x,
										detailLbl.frame.origin.y,
										cellWidth - x - (needNextIcon ? 40 : HO_HORIZONAL_PADDING),
										22);
	detailLbl.text = content;
	if(self.isTranslaterDetail)
	{
		detailLbl.frame = CGRectMake(detailLbl.frame.origin.x,
											(42 - detailLbl.frame.size.height) / 2,
											cellWidth - x - (needNextIcon ? 40 : HO_HORIZONAL_PADDING),
											MAX(detailLbl.frame.size.height, 22));
		contentLbl.frame = CGRectMake(contentLbl.frame.origin.x,
											(42 - contentLbl.frame.size.height) / 2,
											contentLbl.frame.size.width,
											MAX(contentLbl.frame.size.height, 22));
		iconImgView.frame = CGRectMake(iconImgView.frame.origin.x,
									   (42 - iconImgView.frame.size.height) / 2,
									   iconImgView.frame.size.width,
									   iconImgView.frame.size.height);
	}
	else
	{
		
		detailLbl.frame = CGRectMake(x,
											contentLbl.frame.origin.y,
											cellWidth - x - (needNextIcon ? 42 : HO_HORIZONAL_PADDING),
											MAX(detailLbl.frame.size.height, 22));
	}
}

- (CGFloat)getContentHeight
{
	if(detailImgView)
		return HO_CELL_HEIGHT;
	CGFloat height = MAX(detailLbl.frame.size.height, 22) + 2 * detailLbl.frame.origin.y;
	return MAX(height, HO_CELL_HEIGHT);
}

- (CGFloat)getContentHeightWith40Height
{
	CGFloat height = MAX(detailLbl.frame.size.height, 22) + 2 * detailLbl.frame.origin.y;
	return MAX(height, 42);
}

- (void)setFocusable
{
	[editField becomeFirstResponder];
}

- (void)lostFocusable
{
	[editField resignFirstResponder];
}


- (void)setPlaceHolder:(NSString *)placeholder
{
	editField.placeholder = placeholder;
}

- (void)setReturnType:(UIReturnKeyType)type
{
	editField.returnKeyType = type;
}

- (void)setIndexPath:(NSIndexPath *)indexPath
{
	editField.indexPath = indexPath;
}

- (HOTextField *)getTextField
{
	return editField;
}

- (void)dealloc
{
	if(editField)
	{
		[HOUtils removeFromSuperViewForViews:editField];
		[editField release];
	}
	if(detailLbl)
	{
		[HOUtils removeFromSuperViewForViews:detailLbl];
		[detailLbl release];
	}
	if(detailImgView)
	{
		[HOUtils removeFromSuperViewForViews:detailImgView];
		[detailImgView release];
	}
	if(bar)
	{
		[HOUtils removeFromSuperViewForViews:bar];
		[bar release];
	}
	[super dealloc];
}
@end
